<template>
    <div class="app-container">
        <!-- 医院付款管理 -->
        <div class="hospitalPayMent">
            <!-- 查询模块 -->
            <el-form :model="form" ref="form" label-width="130px" :inline="true">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="付款单号">
                            <el-input v-model="form.paymentSn" placeholder="请输入付款单号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="审核状态">
                            <el-select v-model="form.status" placeholder="请选择">
                                <el-option
                                    :label="item.name"
                                    :value="item.value"
                                    v-for="item in reviewStatusList"
                                    :key="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="提交日期" width="236">
                            <el-date-picker
                                value-format="yyyy-MM-dd HH:mm:ss"
                                v-model="dates"
                                type="daterange"
                                range-separator="-"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                :default-time="['00:00:00', '23:59:59']"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" v-if="hasPermission('adminRole')">
                        <el-form-item label="医院名称">
                            <el-select v-model="form.hospitalId" placeholder="请选择" filterable>
                                <el-option
                                    :label="item.name"
                                    v-for="item in hospitalList"
                                    :key="item.value"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" class="alignRight" v-if="hasPermission('adminRole')">
                        <el-button type="primary" @click="handleSearch">查询</el-button>
                        <el-button
                            type="primary"
                            @click="toRoutes('hospitalPayAdd',1)"
                            v-if="!hasPermission('adminRole')"
                        >新增付款单</el-button>
                        <el-button type="primary" @click="handleExport">导出</el-button>
                    </el-col>
                    <el-col :span="6" class="alignRight" v-else>
                        <el-button type="primary" @click="handleSearch">查询</el-button>
                        <el-button
                            type="primary"
                            @click="toRoutes('hospitalPayAdd',1)"
                            v-if="!hasPermission('adminRole')"
                        >新增付款单</el-button>
                        <el-button type="primary" @click="handleExport">导出</el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <!-- 主页列表 -->
        <div class="listDiv" v-loading="tableLoading">
            <el-table :data="tableData" border style="width: 100%" ref="tableData">
                <el-table-column fixed type="index" label="序号" width="50"></el-table-column>
                <el-table-column prop="paymentSn" label="付款单号"></el-table-column>
                <el-table-column prop="hospitalName" label="医院名称"></el-table-column>
                <el-table-column prop="amount" label="付款单据总金额"></el-table-column>
                <!-- <el-table-column prop="paidRatio" label="回款比率"></el-table-column> -->
                <el-table-column prop="paidAmount" label="付款金额"></el-table-column>
                <el-table-column prop="paidCard" label="付款卡号"></el-table-column>
                <el-table-column prop="paidAccount" label="户名"></el-table-column>
                <el-table-column prop="createdTime" label="提交日期"></el-table-column>
                <el-table-column prop="approvedStatus" label="美美咖审核状态">
                    <template slot-scope="scope">{{scope.row.approvedStatus}}</template>
                </el-table-column>
                <el-table-column prop="approvedTime" label="审核通过日期"></el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
                <el-table-column label="操作" width="160" fixed="right">
                    <template slot-scope="scope">
                        <div>
                            <span v-if="scope.row.approvedStatus=='审核驳回'">
                                <el-button
                                    size="small"
                                    type="text"
                                    @click="toRoutes('hospitalPayEdit',scope.row.id)"
                                    v-if="!hasPermission('adminRole')"
                                >编辑</el-button>
                                <el-button
                                    size="small"
                                    type="text"
                                    @click="handleDelete(scope.row.id)"
                                    v-if="!hasPermission('adminRole')"
                                >删除</el-button>
                                <!-- <el-button
                                    size="small"
                                    type="text"
                                    @click="toRoutes('hospitalPayInfo',scope.row.id)"
                                >详情</el-button> -->
                            </span>
                            <span v-else>
                                <el-button
                                    size="small"
                                    type="text"
                                    @click="toRoutes('hospitalPayInfo',scope.row.id)"
                                >详情</el-button>
                            </span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagesCon">
                <div>
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="form.pageNum"
                        :page-sizes="page_sizes"
                        :page-size="10"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="page_totals"
                    ></el-pagination>
                </div>
            </div>
        </div>
        <!-- 分页 -->
    </div>
</template>
<script>
import hospitalPay from "@/api/hospitalPay";
import commonFn from "@/utils/common";
export default {
    name: "hospitalPayIndex",
    data() {
        return {
            tableLoading: true,
            // form表单
            form: {
                paymentSn: "",
                hospitalId: "",
                status: "",
                startTime: null,
                endTime: null,
                pageNum: 1,
                pageSize: 10
            },
            dates: null,
            // 状态下拉选
            reviewStatusList: [
                { name: "全部", value: "" },
                { name: "待审核", value: "0" },
                { name: "审核通过", value: "1" },
                { name: "审核驳回", value: "2" }
            ],
            // 医院下拉选
            hospitalList: [],
            //   table列表数据
            tableData: [],
            currentPage: 1, //当前页数
            page_sizes: [10, 30, 50, 100], //每页显示个数
            page_totals: 60 //总条目数
        };
    },
    inject: ["reload"],
    mounted() {
        if(!this.hasPermission('adminRole')){
            this.form.hospitalId = String(this.$store.state.user.hospitalId) 
        }
        this.getList(this.form);
        this.hospitalNameList();
    },
    methods: {
        // 获取列表
        getList(e) {
            hospitalPay
                .getList(e)
                .then(res => {
                    if (!res.data.code) {
                        // console.log(res);
                        let resData = res.data.data;
                        this.tableData = resData.paidOrderResponseDTOS;
                        this.page_totals = resData.count;
                    } else {
                        this.$message({
                            showClose: true,
                            message: res.data.msg,
                            type: "warning"
                        });
                    }
                    this.tableLoading = false;
                })
                .catch(err => {
                    // this.errorStatus(res.msg);
                    this.tableLoading = false;
                });
        },
        // 查询
        handleSearch() {
            this.form.pageNum = 1
            if (this.dates != null) {
                if (this.dates.length > 0) {
                    this.form.startTime = this.dates[0];
                    this.form.endTime = this.dates[1];
                } else {
                    this.form.startTime = null;
                    this.form.endTime = null;
                }
            } else {
                this.form.startTime = null;
                this.form.endTime = null;
            }
            this.getList(this.form);
        },
        // 导出
        handleExport() {
            let datas = { ...this.form };
            delete datas.pageSize;
            hospitalPay
                .handleExport(datas)
                .then(res => {
                    // console.log(res);
                    commonFn.downFile("医院付款单", res);
                })
                .catch(err => {
                    // this.errorStatus(res.msg);
                });
        },
        // 删除
        handleDelete(e) {
            let defineFn = () => {
                let datas = { id: String(e) };
                hospitalPay
                    .delete(datas)
                    .then(res => {
                        if (!res.data.code) {
                            this.reload();
                            this.succStatus("删除成功");
                        } else {
                            this.$message({
                                showClose: true,
                                message: res.data.msg,
                                type: "warning"
                            });
                        }
                    })
                    .catch(err => {
                        this.errorStatus(res.msg);
                    });
            };
            let confirmDatas = {
                desc: "此操作将会删除该数据, 是否继续?",
                title: "提示",
                confirmBtnTxt: "确定",
                cancelBtnTxt: "取消",
                fn: defineFn,
                catchInfo: "已取消删除"
            };
            this.eleConfirm(confirmDatas);
        },
        handleSizeChange(val) {
            this.form.pageSize = val;
            this.getList(this.form);
        },
        handleCurrentChange(val) {
            this.form.pageNum = val;
            this.getList(this.form);
        },
        // 获取医院名称
        hospitalNameList() {
            hospitalPay
                .hospitalNameList()
                .then(res => {
                    if (!res.data.code) {
                        // console.log(res);
                        let resData = res.data.data;
                        let newArr = [];
                        resData.forEach(e => {
                            newArr.push({
                                name: e.name,
                                value: String(e.hospitalId)
                            });
                        });
                        newArr.unshift({
                            name: "全部",
                            value: ""
                        });
                        this.hospitalList = newArr;
                        // console.log(this.hospitalList);
                    } else {
                        this.$message({
                            showClose: true,
                            message: res.data.msg,
                            type: "warning"
                        });
                    }
                })
                .catch(err => {
                    this.errorStatus(err);
                });
        },
        // 路由跳转
        toRoutes(e, userId) {
            if (userId == 1) {
                let route = { name: e };
                this.$router.push(route);
            } else {
                let route = {
                    name: e,
                    query: { userId: JSON.stringify(userId) }
                };
                this.$router.push(route);
            }
        },
        // 按钮权限
        hasPermission(e) {
            return window.helper.hasPermission(e);
        },
        //confirm框
        eleConfirm(e) {
            this.$confirm(e.desc, e.title, {
                confirmButtonText: e.confirmBtnTxt,
                cancelButtonText: e.cancelBtnTxt,
                type: "warning"
            })
                .then(() => {
                    e.fn();
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: e.catchInfo
                    });
                });
        },
        //成功状态弹窗
        succStatus(txt, status) {
            this.eleMsg(txt, "success");
        },
        //失败状态弹窗
        errorStatus(txt) {
            this.eleMsg(txt, "error");
        },
        //弹窗组件
        eleMsg(txt, status) {
            this.$message({
                showClose: true,
                message: txt,
                type: status
            });
        }
    }
};
</script>
<style scoped>
/* 分页样式 */
.pagesCon {
    margin-top: 20px;
    text-align: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
    width: 236px;
}
.el-col-24 {
    margin-bottom: 20px;
}
</style>